package com.rzcoding.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.rzcoding.dto.base.ResultBean;
import com.rzcoding.dto.base.ResultConst;
import com.rzcoding.dto.dao.ManagerRoleNameMo;
import com.rzcoding.dto.req.UsernamePasswordReqMo;
import com.rzcoding.entity.Manager;
import com.rzcoding.service.ManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;


@RestController
@RequestMapping("/api")
public class ManagerController {

    @Autowired
    private ManagerService managerService;

    @PostMapping(value = "/login" )
    public ResponseEntity login(@Valid UsernamePasswordReqMo userInfo){
        String jwt =  managerService.login(userInfo.username, userInfo.password);
        return new ResponseEntity<>(ResultBean.ok(ResultConst.LOGIN_SUCC, new JSONObject().fluentPut("token", jwt)),
                HttpStatus.OK);
    }

    @GetMapping(value = "/users" )
    public ResponseEntity findPage(@RequestParam(name = "query", required = false) String userName,
                                   @RequestParam(name = "pagenum",required = false,defaultValue = "1") Integer pageNum,
                                   @RequestParam(name = "pagesize",required = false,defaultValue = "10") Integer pageSize){
        PageInfo<ManagerRoleNameMo> pageInfo = managerService.selectByUsername(userName, pageNum, pageSize);
        JSONObject resp = new JSONObject().fluentPut("total", pageInfo.getTotal());
        resp.put("users", pageInfo.getList());
        return new ResponseEntity<>(ResultBean.ok(ResultConst.GET_MANAGERS_SUCC, resp), HttpStatus.OK);
    }

    /***
     * 根据ID删除品牌数据
     * @param id
     * @return
     */
    @DeleteMapping(value = "/users/{id}" )
    public ResponseEntity delete(@PathVariable Integer id){
        managerService.delete(id);
        return new ResponseEntity<>(ResultBean.ok(ResultConst.DEL_MANAGER_SUCC), HttpStatus.OK);
    }

    /***
     * 修改Manager数据
     * @param manager
     * @param id
     * @return
     */
    @PutMapping(value="/users/{id}")
    public ResponseEntity update(Manager manager, @PathVariable Integer id){
        manager.setId(id);
        managerService.update(manager);
        return new ResponseEntity<>(ResultBean.ok(ResultConst.UPD_MANAGER_SUCC), HttpStatus.OK);
    }

    @PutMapping(value="/users/{id}/state/{status}")
    public ResponseEntity updateStatus(@PathVariable Integer id, @PathVariable Boolean status){
        Manager manager = new Manager();
        manager.setId(id);
        manager.setState(status);
        managerService.update(manager);
        return new ResponseEntity<>(ResultBean.ok(ResultConst.UPD_USER_STATUS_MANAGER_SUCC), HttpStatus.OK);
    }

    @PutMapping(value="/users/{id}/role")
    public ResponseEntity updateRole(@RequestParam(name = "rid") Integer rid, @PathVariable Integer id){
        Manager manager = new Manager();
        manager.setId(id);
        manager.setRoleId(rid);
        managerService.update(manager);
        return new ResponseEntity<>(ResultBean.ok(ResultConst.UPD_USER_ROLE_MANAGER_SUCC), HttpStatus.OK);
    }

    /***
     * 新增Manager数据
     * @param manager
     * @return
     */
    @PostMapping(value = "/users")
    public ResponseEntity add(Manager manager){
        managerService.add(manager);
        return new ResponseEntity<>(ResultBean.ok(ResultConst.ADD_MANAGER_SUCC), HttpStatus.OK);
    }

    /***
     * 根据ID查询Manager数据
     * @param id
     * @return
     */
    @GetMapping("/users/{id}")
    public ResponseEntity findById(@PathVariable Integer id){
        Manager manager = managerService.findById(id);
        return new ResponseEntity<>(ResultBean.ok(ResultConst.GET_MANAGER_SUCC, manager), HttpStatus.OK);
    }
}
